﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RegistPelicula : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
        
    String SQL, mssg;
    AdmiDatos.Service ad = new AdmiDatos.Service();
    System.Data.DataSet dst = new System.Data.DataSet();
    Formato1 vt = new Formato1();

    private void habilitarExiste()
    {
        BtnAgregar.Enabled = false;
        BtnModificar.Enabled = true;
        BtnEliminar.Enabled = true;
    }
    private void habilitarNuevo()
    {
        BtnAgregar.Enabled = true;
        BtnModificar.Enabled = false;
        BtnEliminar.Enabled = false;
    }
    private void deshabilitarBotones()
    {
        BtnAgregar.Enabled = false;
        BtnModificar.Enabled = false;
        BtnEliminar.Enabled = false;
        vt.LimpiartextBoxes(this);
    }

    protected void BtnAgregar_Click(object sender, EventArgs e)//No agrega
    {
        
        try
        {
            string fechaC = DatePicker1.SelectedDate.ToString("yyyy-MM-dd");
            SQL = "Insert into Pelicula(PeliId,Titulo,FechaCreacion,Duracion,PrecioCosto,CategoriaId,PrecioAlquiler)";
            SQL += " Values ('" + TxtIdpeli.Text + "','" + TxtTitulo.Text + "','";
            SQL += fechaC + "','" + TxtDuracion.Text + "','" + TxtRecaudado.Text + "','";
            SQL += TxtIdCateg.Text + "','" + TxtPrecioA.Text + "')";
            Response.Write(SQL);            
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("Los Registros han sido ingresado exitosamente!", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }

    }
    protected void BtnModificar_Click(object sender, EventArgs e)//No modifica
    {
        try
        {
            String fechaC = DatePicker1.SelectedDate.ToString();
            SQL = "Update Pelicula set Titulo ='" + TxtTitulo.Text + "'";
            SQL += ",FechaCreacion = '" + fechaC + "'";
            SQL += ",Duracion = '" + TxtDuracion.Text + "'";
            SQL += ",PrecioCosto = '" + TxtRecaudado.Text + "'";
            SQL += ",PrecioAlquiler = '" + TxtPrecioA.Text + "'";
            SQL += " Where PeliId = '" + TxtIdpeli.Text + "'";
            Response.Write(SQL);
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("El Registro ha sido modificado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }
    protected void BtnEliminar_Click(object sender, EventArgs e)
    {
        try
        {
            SQL = "Delete from Pelicula Where PeliId = '" + TxtIdpeli.Text + "'";
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("Los Registros se han eliminado exitosamente!", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            string fechaC = DatePicker1.SelectedDate.ToString();
            SQL = "Select * From Pelicula Where PeliId = '" + TxtIdpeli.Text + "'";
             ad.ObtenerD(ref mssg, SQL, ref dst);
            if (dst.Tables[0].Rows.Count > 0)
            {
                TxtTitulo.Text = dst.Tables[0].Rows[0][1].ToString();
                TxtDuracion.Text = dst.Tables[0].Rows[0][3].ToString();
                TxtRecaudado.Text = dst.Tables[0].Rows[0][4].ToString();
                TxtPrecioA.Text = dst.Tables[0].Rows[0][6].ToString();
                TxtIdCateg.Text = dst.Tables[0].Rows[0][5].ToString();
                DatePicker1.SelectedDate = Convert.ToDateTime(dst.Tables[0].Rows[0][2].ToString());
                habilitarExiste();
                
            }
            else
            {
                vt.MessageBox("Registro no encontrado!", this);
                habilitarNuevo();
                Session["IdPelicula"] = TxtIdpeli.Text;
                vt.LimpiartextBoxes(this);
                TxtIdpeli.Text = Session["IdPelicula"].ToString();
            }
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
                    
    }
}//Cierre de class